import '../public/index'
import url from '../data/url'
import request from '../data/request'
import {tableConfig, header} from '../data/config'
import store from 'store'
import {getUrlParam} from '../data/util'

const base = url.base

let table
let storeData

let param = getUrlParam('house')

$.ajax({
    type: "GET",
    url: base + '/admin/house/facade.json?buildId=' + param,
    dataType: "json",
    headers: {
        myToken: Cookies.get('myToken')
    },
}).then((res) => {
    //存信息到store
    storeData = res
    store.set('house', storeData)

    //添加房屋信息
    let str = `<div class="name">${res.hsList.buildName}</div>
                <div class="house-num">${res.hsList.hsBuildNum}</div>
                <div class="sold-num">${res.hsList.hsSoldNum}</div>
                <div class="overage">${res.hsList.hsBuildNum - res.hsList.hsSoldNum}</div>`
    $('#house-info').append(str)

    let roofBeamArr = []
    res.hbuList.forEach(k => {
        roofBeamArr.push(k)
    })

    //推送楼栋信息方法
    function createRoofBeamDom(k) {
        let str = `<div>${k.hbuNumStr}</div>`
        return str
    }

    roofBeamArr.forEach((k) => {
        $("#roof-beam .roof-list").append(createRoofBeamDom(k))
    })

    let listArr = res.htList
    store.set('houseListArr', listArr)

    //初始化表单方法
    let tableInit = ()=>{
        let tableArr = [
            [{
                field: 'houseType',
                title: '楼盘名称',
                valign: 'middle'
            }],
            [{
                field: 'setInArea',
                title: '楼盘名称',
                valign: 'middle'
            }],
            [{
                field: 'buildArea',
                title: '楼盘名称',
                valign: 'middle'
            }]
        ]

        let rendenTableArr = [{
            field: 'nothing',
            title: '',
            valign: 'middle',
            colspan: 1
        }]
        let setInAreaArr = [{
            field: 'nothing',
            title: '',
            valign: 'middle',
            colspan: 1
        }]
        let buildAreaArr = [{
            field: 'nothing',
            title: '',
            valign: 'middle',
            colspan: 1
        }]

        //户型信息
        let houseType = (k, index) => {
            let obj = {
                field: 'houseType',
                title: index + 1 + '号' + '（' + k.htStructure + '）' + '' + k.typeName,
                valign: 'middle',
                colspan: 4
            }
            return obj
        }

        //建面信息
        let setInArea = (k, index) => {
            let obj = {
                field: 'setInArea',
                title: k.setInArea,
                valign: 'middle',
                colspan: 2

            }
            return obj
        }
        //套内信息
        let buildArea = (k, index) => {
            let obj = {
                field: 'buildArea',
                title: k.buildArea,
                valign: 'middle',
                colspan: 2
            }
            return obj
        }

        listArr.forEach((k, index) => {
            rendenTableArr.push(houseType(k, index))
        })
        //表单第一行
        tableArr[0] = rendenTableArr
        //表单第二行,建筑面积信息
        listArr.forEach((k, index) => {
            setInAreaArr.push({
                field: 'setInArea',
                title: '建筑面积',
                valign: 'middle',
                colspan: 2
            })
            setInAreaArr.push(setInArea(k, index))
        })
        tableArr[1] = setInAreaArr
        //表单第三行，套内面积
        listArr.forEach((k, index) => {
            buildAreaArr.push({
                field: 'setInArea',
                title: '套内面积',
                valign: 'middle',
                colspan: 2
            })
            buildAreaArr.push(buildArea(k, index))
        })
        tableArr[2] = buildAreaArr

        console.log(tableArr)
        //每个楼层单独信息
        let floors = {
            totalFloor: null,
            allFloor: []
        } //楼层信息
        let allHouses = res.hList

        //获取层高信息
        floors.totalFloor = allHouses[allHouses.length - 1].floor
        floors.allFloor.length = floors.totalFloor
        //设置表单显示
        allHouses.forEach((k, index) => {
            /*** 每个楼层数据 ***/
            let roofBeam = {
                field: 'roofInfo',
                title: '1-1-1',
                valign: 'middle',
                colspan: 1
            }

            let price = {
                field: 'price',
                title: k.hosUnitPrice / 100 + '.00' + '元／㎡ ',
                valign: 'middle',
                colspan: 2
            }

            let thisSta
            switch (k.status) {
                case 0:
                    thisSta = '待售'
                    break
                case 1:
                case 2:
                    thisSta = '在售'
                    break
                case 3:
                    thisSta = '已售'
                    break
            }
            let status = {
                field: 'status',
                title: thisSta,
                valign: 'middle',
                colspan: 1
            }
            if (!floors.allFloor[k.floor - 1]) {
                floors.allFloor[k.floor - 1] = []
                floors.allFloor[k.floor - 1].push(
                    {
                        field: 'status',
                        title: k.floor + 'F',
                        valign: 'middle',
                        colspan: 1
                    }
                )
                floors.allFloor[k.floor - 1].push(roofBeam, price, status)
            } else {
                floors.allFloor[k.floor - 1].push(roofBeam, price, status)
            }
        })
        let renderenArr = tableArr.concat(floors.allFloor)
        console.log(renderenArr)
        //启动表单
        $('#table').bootstrapTable({
            columns: renderenArr
        })

    }

    //运行表单
    tableInit()
})

// 点击列表的时候传递数据到全局变量
table.on('click-row.bs.table', (e, data, el) => {
    storeData = data
})